package com.cheyun.netsalev3.repository

import androidx.lifecycle.MutableLiveData
import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.ClueParam
import com.cheyun.netsalev3.bean.Row
import com.cheyun.netsalev3.bean.basedata.NormalList
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class HomeCustomerPageRepository(val nkey: String,
                                 var map: Map<String, String> = HashMap(),
                                 var boolean: Boolean = false,
                                 var enable:Boolean = true) : BasePageRepository<Row>() {

    var toplist = MutableLiveData<List<NormalList>>()
    var focus_total = MutableLiveData<Int>()
    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }

    fun setEanbleLoad(e:Boolean){
        this.enable = e
    }
   // var total = MutableLiveData<Int>()
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<Row>) -> Unit) {
       if(!this.enable)return
       DemoLog.d("api/v2.archive/datalist",String.format("loadInitial:nkey(%s),page(%s),map(%s)",nkey,1,map.toString()))
        ApiClient.getInstance().getClueList(1, nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueParam>>() {
                    override fun onSuccess(t: BaseResp<ClueParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            focus_total.postValue(t.data!!.focus_total)
                            operData(this, t.data!!.total)
                            toplist.postValue(t.data!!.toplist)
                            for (item in this) {
                                item.setData(t.data!!.total, nkey, boolean)
                            }
                            callback(this)
                        }

                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }


    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Row>) {
        DemoLog.d("api/v2.archive/datalist",String.format("loadAfter:nkey(%s),page(%s),map(%s)",nkey,params.key,map.toString()))
        ApiClient.getInstance().getClueList(params.key, nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueParam>>() {
                    override fun onSuccess(t: BaseResp<ClueParam>) {
                        t.data?.rows?.apply {
                            for (item in this) {
                                item.setData(t.data!!.total, nkey, boolean)
                            }
                            operData(this, t.data!!.total)
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Row>) {
        DemoLog.d("api/v2.archive/datalist",String.format("loadBefore:nkey(%s),page(%s),map(%s)",nkey,params.key,map.toString()))
        ApiClient.getInstance().getClueList(params.key, nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueParam>>() {
                    override fun onSuccess(t: BaseResp<ClueParam>) {
                        t.data?.rows?.apply {
                            for (item in this) {
                                item.setData(t.data!!.total, nkey, boolean)
                            }
                            operData(this, t.data!!.total)
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun operData(data: MutableList<Row>, total: Int) {
        var clueColumn = MySharedPreferences.getClueRightColumn()
        for (item in data) {
            item.setData(total, nkey, boolean)
//            item.hasFollow = if (item.followname.isEmpty()) 0 else 1
            when (clueColumn) {
                "dateline" -> item.clueCustomerColumn = if (item.dateline == null) "" else item.dateline
                "infotypename" -> item.clueCustomerColumn = if (item.infotypename == null) "" else item.infotypename
                "infoplacename" -> item.clueCustomerColumn = if (item.infoplacename == null) "" else item.infoplacename
                "infosourcename" -> item.clueCustomerColumn = if (item.infosourcename == null) "" else item.infosourcename
                "brandname" -> item.clueCustomerColumn = if (item.brandname == null) "" else item.brandname
                "specname" -> item.clueCustomerColumn = if (item.specname == null) "" else item.specname
                "region" -> item.clueCustomerColumn = if (item.region == null) "" else item.region
                "exteriorname" -> item.clueCustomerColumn = if (item.exteriorname == null) "" else item.exteriorname
                "interiorname" -> item.clueCustomerColumn = if (item.interiorname == null) "" else item.interiorname
                "customerlevelname" -> item.clueCustomerColumn = if (item.customerlevelname == null) "" else item.customerlevelname
            }
//            item.choosed = if(checkStatus == 0) 0 else 2

        }
    }


    fun setIsFocus(tid: String, isFocus: Int, moth: () -> Unit) {
        ApiClient.getInstance().isFocus(tid, if (isFocus == 1) "cancel" else "attention")
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

}